<?php if ( ! defined('APPPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/

class album_controller extends PZ_Controller{

	public function __construct() {
		parent::__construct();
		$this->authfunctions = array('listuseralbums','createalbum','removelike','addlike');
	}
	//$postvar = array('uialbum_numperpage'=>'20','uialbum_minnum'=>'1','uialbum_layout'=>'4','uialbum_animated'=>'0','uialbum_auto'=>'1');
	public function index(){
		$settingapi = load_api('setting');
		$setting = $settingapi->getSetting('layoutalbum');

		$num_per_page = $setting['uialbum_numperpage']?$setting['uialbum_numperpage']:20;
		$numbershowpage = $setting['uialbum_numbershowpage']?$setting['uialbum_numbershowpage']:0;

		$page = $this->param('page','1');
		$wpage = $this->param('wpage');
		$args = array("page"=>"2","wf"=>"1");
		if($wpage&&$numbershowpage){
			$data['page'] = ($wpage-1)*$numbershowpage+$page;
			$args['wpage'] = $data['wpage'] = $wpage;
		}else{
			$data['page'] = $page;
		}
		$data['showpage'] = (($numbershowpage)&&($data['page']%$numbershowpage==0))?'1':'0';
		
		
		$conditions['total_share_num'] = $setting['uialbum_minnum']?$setting['uialbum_minnum']:1;

		$order =  $this->param("sort",'time');
		$sort = null;
		if(in_array($order, array('time','share','like','uptime'))){
			if($order=='time'){
				$sort = ' ptx_album.create_time DESC ';
			}else if($order=='share'){
				$sort = ' ptx_album.total_share DESC ';
			}else if($order=='like'){
				$sort = ' ptx_album.total_like DESC ';
			}else if($order=='uptime'){
				$sort = ' ptx_album.update_time DESC ';
			}
			$args['sort'] = $order;
			$data['sort'] = $order;
		}

		$data['nextpage_url'] = site_url_param('album/index',$args);

		$api = load_api('album');
		$albums = $api->search($conditions,$data['page'],$num_per_page,$sort);
		$pager = &load_class('Pager', 'core');
		$data['pages'] = $pager->createPages('album/index',$args);
		unset($args['wf']);
		unset($args['page']);
		$data['wpages'] = $pager->createCustomPages('album/index',$args,'wpage',$num_per_page*$numbershowpage);
		foreach ($albums as $k=>$album){
			$albums[$k] = $api->prepareAlbum($album);
		}
		$data['tpl_waterfall'] = $this->render('waterfall/album',array('albums'=>$albums,'showpage'=>$data['showpage']));
		//$data['pinsetting'] = $setting;
		$data['albumsetting'] = $setting;
		if($data['page']==1){
			$this->load_seo('album');
		}
		$this->output("album/index",$data,($this->param('wf'))?false:true);
	}

	public function listuseralbums(){
		$api = load_api('album');
		$category_id = $this->param('cid');
		$uid = $this->param('uid')?$this->param('uid'):$this->cuser['uid'];
		$albums = $api->getAlbumsByUser($uid,$category_id);
		if($albums){
			$this->ajax_success_response($albums, '');
		}else{
			$this->ajax_failed_response();
		}
		return;
	}


	public function removelike(){
		if($aid = $this->param('aid')){
			$albumapi = load_api('album');
			$album = $albumapi->findAlbumByid($aid);

			if($album['user_id'] == $this->cuser['uid']){
				$this->ajax_failed_response('like_self');
				return;
			}

			$favalbumdb = load_db('album/favalbum');
			$liked = $favalbumdb->find(array('album_id'=>$aid,'user_id'=>$this->cuser['uid']));
			if($liked){
				$favalbumdb->delete($liked);
				load_db('album')->remove_like($aid);
				$this->ajax_success_response(null, 'success');
			}else{
				$this->ajax_failed_response('not_liked');
			}
		}else{
			$this->ajax_failed_response(lang('500_postfaild','tip'));
		}
	}
	public function addlike(){
		if($aid = $this->param('aid')){
			$albumapi = load_api('album');
			$album = $albumapi->findAlbumByid($aid);

			if($album['user_id'] == $this->cuser['uid']){
				$this->ajax_failed_response('like_self');
				return;
			}
			$favalbumdb = load_db('album/favalbum');
			$liked = $favalbumdb->find(array('album_id'=>$aid,'user_id'=>$this->cuser['uid']));
			if($liked){
				$this->ajax_failed_response('like_already');
				return;
			}
			$addlike_result = load_db('album')->add_like($aid);
			$result = $favalbumdb->add_one(array('album_id'=>$aid,'user_id'=>$this->cuser['uid']));
			load_api('event')->addlikealbum($this->cuser['uid'],$album['album_title'],$aid,$album['user_id']);
		}
		if ($result) {
			$this->ajax_success_response(null);
		}else{
			$this->ajax_failed_response('Add Like Faild');
		}

	}

}



